package hxg.面试题.test.jdk8新特性.新时间控件;

import java.time.*;

/**
 * @author HeXiangGuang
 * @create 2022-10-22 21:16
 */
public class dataTimeAPI {
    public static void main(String[] args) {
        //1.Clock
        final Clock clock = Clock.systemUTC();
        System.out.println( clock.instant() );//2020-12-24T07:32:56.132Z
        System.out.println( clock.millis() );//1608795176247

//2. ISO-8601格式且无时区信息的日期部分
        final LocalDate date = LocalDate.now();
        final LocalDate dateFromClock = LocalDate.now( clock );

        System.out.println( date );//2020-12-24
        System.out.println( dateFromClock );//2020-12-24

// ISO-8601格式且无时区信息的时间部分
        final LocalTime time = LocalTime.now();
        final LocalTime timeFromClock = LocalTime.now( clock );

        System.out.println( time );//15:32:56.257
        System.out.println( timeFromClock );//15:32:56.257

// 3.ISO-8601格式无时区信息的日期与时间
        final LocalDateTime datetime = LocalDateTime.now();
        final LocalDateTime datetimeFromClock = LocalDateTime.now( clock );

        System.out.println( datetime );//2020-12-24T15:32:56.257
        System.out.println( datetimeFromClock );//2020-12-24T15:32:56.257

// 4.特定时区的日期/时间，
        final ZonedDateTime zonedDatetime = ZonedDateTime.now();
        final ZonedDateTime zonedDatetimeFromClock = ZonedDateTime.now( clock );
        final ZonedDateTime zonedDatetimeFromZone = ZonedDateTime.now( ZoneId.of( "America/Los_Angeles" ) );

        System.out.println( zonedDatetime );//2020-12-24T15:32:56.258+08:00[Asia/Shanghai]
        System.out.println( zonedDatetimeFromClock );//2020-12-24T07:32:56.258Z
        System.out.println( zonedDatetimeFromZone );//2020-12-23T23:32:56.260-08:00[America/Los_Angeles]

//5.在秒与纳秒级别上的一段时间
        final LocalDateTime from = LocalDateTime.of( 2014, Month.APRIL, 16, 0, 0, 0 );
        final LocalDateTime to = LocalDateTime.of( 2015, Month.APRIL, 16, 23, 59, 59 );

        final Duration duration = Duration.between( from, to );
        System.out.println( "Duration in days: " + duration.toDays() );//365
        System.out.println( "Duration in hours: " + duration.toHours() );//8783
    }
}
